#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import csv
import shutil
from B2M import B2M

def classifyToImg(source,destination):
  num = 0
  for fpathe,dirs,fs in os.walk(source):
    for f in fs:
      source_file = os.path.join(fpathe,f)
      num = num +1
      with open('ExploitDB_Info.csv', 'r') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
          if row['app_url'].split("/")[4] == f:
            dir = row["type"] + "\\" + row["platform"] + "\\"
            for category in row["category"].split(" / "):
              # file_name = destination + "\\" + dir + str(category).replace(" ","").replace(".","_").replace("/","_").replace(":","_").replace("?","_").replace("\"","\'") + "\\" + row["app_url"].split("/")[-1]
              # mycopyfile(source_file, file_name)
              img_name = destination + "Img" + "\\" + dir + str(category).replace(" ","").replace(".","_").replace("/","_").replace(":","_").replace("?","_").replace("\"","\'") + "\\" + str(os.path.splitext(row["app_url"])[0]).split("/")[-1].replace(".tar","")+".png"
              B2M(source_file,img_name)

  print("classifyToImg num:",num)

#classifyToImg('E:\\我的文档\\毕业论文\\漏洞\\Download','.\\Download')

def mycopyfile(srcfile, dstfile):

  if os.path.isfile(dstfile):
    print("%s has been existed!" % (dstfile))
    return

  if not os.path.isfile(srcfile):
    print("%s not exist!" % (srcfile))
  else:
    fpath, fname = os.path.split(dstfile)  # 分离文件名和路径
    if not os.path.exists(fpath):
      os.makedirs(fpath)  # 创建路径
    shutil.copyfile(srcfile, dstfile)  # 复制文件
    print("copy %s -> %s" % (srcfile, dstfile))

